/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IBusinessCodeDao;
import com.ce.pms.charge.dao.IChargeItemDao;
import com.ce.pms.charge.dao.IParameterDefDao;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.model.ChargeItemDomain;
import com.ce.pms.charge.model.ParameterDefDomain;
import com.ce.pms.charge.service.IParameterDefService;
import com.ce.pms.charge.vo.ParameterDefVO;
import com.ce.webframework.exception.BusinessException;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：参数预设,此模块方法与费用模块相关，如果其它模块要用，考虑重构本类<br>
 * @author 罗军林 创建日期：2008-8-6
 */
public class ParameterDefServiceImpl extends PmsBaseServiceImpl implements
		IParameterDefService {
	IParameterDefDao parameterDefDao;
	IBusinessCodeDao businessCodeDao;
	IChargeItemDao chargeItemDao;

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.IParameterDefService#editParameterDef(com.ce.pms.charge.vo.ParameterDefVO)
	 */
	public ParameterDefVO editParameterDef(ParameterDefVO parameterDefVO) {
		checkObject(parameterDefVO);
		checkKeyword(parameterDefVO.getParameterDefId());

		ParameterDefDomain parameterDefDomain = parameterDefDao.findById(parameterDefVO.getParameterDefId());
		convertVoToDo(parameterDefVO, parameterDefDomain);
		this.modifyInfo(parameterDefDomain.getSysInfo(), parameterDefVO.getCreator());

		return convertDoToVo(parameterDefDomain);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.IParameterDefService#findParameterDefByKeyword(java.lang.Long)
	 */
	public ParameterDefVO findParameterDefByKeyword(Long keyword) {
		checkKeyword(keyword);

		ParameterDefDomain parameterDefDomain = parameterDefDao.findById(keyword);
		return convertDoToVo(parameterDefDomain);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.IParameterDefService#queryParameterDef(com.ce.pms.charge.vo.ParameterDefVO, int, int)
	 */
	public Page queryParameterDef(ParameterDefVO condition, int pageNo,
			int pageSize) {
		Page page = parameterDefDao.queryParameterDef(condition, pageNo, pageSize);
		page.setResult( convertDoToVOList(page.getResult()) );

		return page;
	}

	public List<ParameterDefDomain> getNotInCode(String temStr){
	return parameterDefDao.getNotInCode(temStr);
	}


	/** 功能描述:DO列表转VO列表，注本方法与费用模块相关<br>
	 * @param list
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<ParameterDefVO> convertDoToVOList(List<ParameterDefDomain> list) {
		List<ParameterDefVO> temp=new ArrayList<ParameterDefVO>();
		for(ParameterDefDomain p : list){
			temp.add(convertDoToVo(p));
		}
		return temp;
	}

	/**
	 * 功能描述:VO转DO，如果VO中的某个字段为null，将不会改动<br>
	 * @param parameterDefVO
	 * @param parameterDefDomain<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void convertVoToDo(ParameterDefVO parameterDefVO,ParameterDefDomain parameterDefDomain){
		if(parameterDefVO == null || parameterDefDomain == null){
			return;
		}
		if(parameterDefVO.getDescription() != null){
			parameterDefDomain.setDescription(parameterDefVO.getDescription());
		}
		if(parameterDefVO.getGkey() != null){
			parameterDefDomain.setGkey( parameterDefVO.getGkey() );
		}
		if(parameterDefVO.getPkey() != null){
			parameterDefDomain.setPkey( parameterDefVO.getPkey() );
		}
		if(parameterDefVO.getPname() != null){
			parameterDefDomain.setPname( parameterDefVO.getPname() );
		}
		if(parameterDefVO.getPvalue() != null){
			parameterDefDomain.setPvalue( parameterDefVO.getPvalue() );
		}
	}

	/**
	 * 功能描述:DO转VO，注本方法与费用模块相关<br>
	 * @param parameterDefDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private ParameterDefVO convertDoToVo(ParameterDefDomain parameterDefDomain){
		ParameterDefVO parameterDefVO=new ParameterDefVO();
		if(parameterDefDomain.getSysInfo() != null)
			parameterDefVO.setCreator(parameterDefDomain.getSysInfo().getCreator());
		parameterDefVO.setDescription(parameterDefDomain.getDescription());
		parameterDefVO.setGkey(parameterDefDomain.getGkey());
		parameterDefVO.setParameterDefId(parameterDefDomain.getParameterDefId());
		parameterDefVO.setPvalue(parameterDefDomain.getPvalue());
		parameterDefVO.setPkey(parameterDefDomain.getPkey());
		parameterDefVO.setPname(parameterDefDomain.getPname());
		if(parameterDefDomain.getPvalue() != null ){
			if(parameterDefDomain.getPvalueType().equals(ParameterDefDomain.PVALUE_TYPE_BUSSINESSCODE)){
				BusinessCodeDomain businessCodeDomain = businessCodeDao.findById(parameterDefDomain.getPvalue());
				parameterDefVO.setPvalueView(businessCodeDomain.getBizCode());
			}else if(parameterDefDomain.getPvalueType().equals(ParameterDefDomain.PVALUE_TYPE_CHARGEITEM)){
				ChargeItemDomain chargeItemDomain=chargeItemDao.findById(parameterDefDomain.getPvalue());
				parameterDefVO.setPvalueView(chargeItemDomain.getItemName());
			}
		}
		return parameterDefVO;
	}

	/**
	 * 功能描述:检查主键是否为空<br>
	 * @param parameterDefVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkKeyword(Object parameterDefId){
		if(parameterDefId==null){
			throw new BusinessException("charge:parameterDefId_Not_Null",BusinessException.ERROR);
		}
	}

	/**
	 * 功能描述:检查对象是否为空<br>
	 * @param o<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-6<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkObject(Object o){
		if(o==null){
			throw new BusinessException("common:unknown_exception",BusinessException.ERROR);
		}
	}


	public IParameterDefDao getParameterDefDao() {
		return parameterDefDao;
	}

	public void setParameterDefDao(IParameterDefDao parameterDefDao) {
		this.parameterDefDao = parameterDefDao;
	}

	/**
	 * @return businessCodeDaogetBusinessCodeDao
	 */
	public IBusinessCodeDao getBusinessCodeDao() {
		return businessCodeDao;
	}

	/**
	 * @param businessCodeDao the businessCodeDao to set
	 */
	public void setBusinessCodeDao(IBusinessCodeDao businessCodeDao) {
		this.businessCodeDao = businessCodeDao;
	}

	/**
	 * @param chargeItemDao the chargeItemDao to set
	 */
	public void setChargeItemDao(IChargeItemDao chargeItemDao) {
		this.chargeItemDao = chargeItemDao;
	}

	public ParameterDefVO findParameterDefByPkey(String pkey) {
		ParameterDefDomain parameterDefDomain = this.parameterDefDao.findUniqueBy("pkey", pkey);
		return convertDoToVo(parameterDefDomain);
	}




}
